package com.tz.farmer.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.enums.ErrorCodeEnum;
import com.tz.farmer.service.FarmerUserInfoService;
import com.tz.farmer.so.FarmerUserInfoSo;
import com.tz.farmer.util.POIStyleImpl;
import com.tz.farmer.util.ResultExM;
import com.tz.farmer.vo.FarmerUserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @ClassName SmartAdminUserInfoController
 * @Description TODO
 * @Creator kaikai
 * @Date 2021/5/11 19:04
 */
@Api(tags = "admin用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class FarmerUserInfoController {

    Logger logger = LoggerFactory.getLogger(FarmerUserInfoController.class);

    @Resource
    FarmerUserInfoService farmerUserInfoService;

    /**
     * 添加查询列表
     * @param so
     * @return
     */
    @GetMapping("/listConditions")
    @ApiOperation("用户条件查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userAccount", value = "登陆账号", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "userPhone", value = "登陆手机号", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "currentPage", value = "当前页", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "PageSize", value = "每页条数", dataType = "string", defaultValue = "", required = true)
    })
    public ResultEx getAdminRoleListByConditions(FarmerUserInfoSo so){
        return farmerUserInfoService.getAdminUserListByConditions(so);
    }

    /**
     * 添加修改软删除
     * @param so
     * @return
     */
    @PostMapping("/editAdd")
    @ApiOperation("添加修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "操作表示1：添加 2：修改", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "id", value = "自增id", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "userAccount", value = "登陆账号", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "userPhone", value = "登陆手机号", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "business", value = "源头主体企业", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "password", value = "登陆密码", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "address", value = "县区", dataType = "string", defaultValue = "", required = true),
            @ApiImplicitParam(name = "isDelete", value = "是否删除 0:未删除 1:删除", dataType = "int", defaultValue = "", required = true),
            @ApiImplicitParam(name = "isUse", value = "是否删除 0:正常 1:禁用", dataType = "int", defaultValue = "", required = true)
    })
    public ResultEx  updateAdminRoleInfo(@RequestBody  FarmerUserInfoSo so){
        return farmerUserInfoService.updateAdminUserInfo(so);
    }

    /**
     * 下载
     * @param so
     * @return
     */
    @GetMapping("/listConditionsDownLoad")
    @ApiOperation("用户条件查询列表下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "userAccount", value = "登陆账号", dataType = "string", defaultValue = "", required = false),
            @ApiImplicitParam(name = "userPhone", value = "登陆手机号", dataType = "string", defaultValue = "", required = false)
    })
    public ResultEx getCodeByProductId(@ApiIgnore @Validated FarmerUserInfoSo so, HttpServletResponse resp) {
        try {
            List<FarmerUserInfoVo> data = farmerUserInfoService.getUserDownLoadList(so);
            if (data.size() <= 0) {
                return ResultExM.builder().makeResultNoData();
            }
            ExportParams exportParams = new ExportParams();
            exportParams.setStyle(POIStyleImpl.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, FarmerUserInfoVo.class, data);
            // 告诉浏览器用什么软件可以打开此文件
            resp.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            resp.setHeader("Content-Disposition", "attachment;filename=" + new String("用户表".getBytes(StandardCharsets.UTF_8), "iso8859-1") + ".xls");
            //编码
            resp.setCharacterEncoding("UTF-8");
            workbook.write(resp.getOutputStream());
            return null;
        } catch (Exception ex) {
            log.error("导出失败：", ex);
            return ResultExM.builder().makeResult(ErrorCodeEnum.ERR_COMMON_ERROR_EXPORT);
        }
    }


    /**
     * 添加查询列表
     * @param so
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", dataType = "int", defaultValue = "", required = false),
    })
    public ResultEx deleteUser(FarmerUserInfoSo so){
        return farmerUserInfoService.deleteUser(so);
    }


    /**
     * 查询源头企业下拉列表
     * @param
     * @return
     */
    @GetMapping("/businessSelect")
    @ApiOperation("查询源头企业下拉列表")

    public ResultEx businessSelect(@Param("request") HttpServletRequest request) {
        String userAccount = request.getHeader("userAccount");
        logger.info("用户账号为：：：：" + userAccount);
        return farmerUserInfoService.businessSelect(userAccount);
    }



    /**
     * 修改密码
     * @param so
     * @return
     */
        @PostMapping("/editPassword")
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", dataType = "String", defaultValue = "", required = true),
            @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String", defaultValue = "", required = true),
            @ApiImplicitParam(name = "confirmNewPassword", value = "确认新密码", dataType = "String", defaultValue = "", required = true)
    })
    public ResultEx  editPassword(@RequestBody  FarmerUserInfoSo so, @Param("request") HttpServletRequest request){
        String userAccount=request.getHeader("userAccount");
        so.setUserAccount(userAccount);
        return farmerUserInfoService.editPassword(so);
    }

}
